#include "AERenderingEngine.hpp"
#include "AESettingsManager.hpp"
#include "AELogManager.hpp"

#include "resource.h"
#include "version.h"

using namespace Ogre;
using namespace arete::io;

BEGIN_ARETE_NAMESPACE(rendering)

	AERenderingEngine::AERenderingEngine(void) :
	m_root(NULL),
	m_renderer(NULL),
	m_window(NULL),
	m_timer(NULL),
	m_scenemanager(NULL),
	m_has_focus(EWF_NO)
{
}

AERenderingEngine::~AERenderingEngine(void)
{
	delete m_root;
	m_root = NULL;
	m_renderer = NULL;
	m_window = NULL;
	m_timer = NULL;
	m_scenemanager = NULL;
}

void AERenderingEngine::initialize(void)
{
	AELogManager::log(ELL_NONE,"*****************************************");
	AELogManager::log(ELL_NONE,"*** ---   Initializing Renderer   --- ***");
	AELogManager::log(ELL_NONE,"*****************************************");

	m_root = new Root("","","");

	initializeResouceGroups();
	initializePlugins();
	initializeRenderSubsytem();
	initializeWindow();
	setIcon();

	ResourceGroupManager::getSingleton().initialiseAllResourceGroups();

	WindowEventUtilities::addWindowEventListener(m_window,this);

	m_scenemanager = m_root->createSceneManager(ST_GENERIC,"Default");
	m_timer = m_root->getTimer();
}

void AERenderingEngine::takeScreenShot( void )
{
}

Ogre::Root* AERenderingEngine::getRoot(void)
{
	return m_root;
}

Ogre::RenderSystem* AERenderingEngine::getRenderer(void)
{
	return m_renderer;
}

Ogre::RenderWindow* AERenderingEngine::getWindow(void)
{
	return m_window;
}

Ogre::Timer* AERenderingEngine::getTimer(void)
{
	return m_timer;
}

Ogre::SceneManager* AERenderingEngine::getSceneManager(void)
{
	return m_scenemanager;
}

void AERenderingEngine::initializeResouceGroups( void )
{
	ResourceGroupManager::getSingleton().addResourceLocation("data/","FileSystem","Data",true);
	ResourceGroupManager::getSingleton().addResourceLocation(AESettingsManager::getSingleton().getSettingAsString("Application","UserDir"),"FileSystem","User",true);
}

void AERenderingEngine::initializePlugins( void )
{
#ifdef _DEBUG
	m_root->loadPlugin("plugins/RenderSystem_GL_d");
	m_root->loadPlugin("plugins/Plugin_CgProgramManager_d");
	m_root->loadPlugin("OgreOggSound_d");
#else
	m_root->loadPlugin("plugins/RenderSystem_GL");
	m_root->loadPlugin("plugins/Plugin_CgProgramManager");
	m_root->loadPlugin("OgreOggSound");
#endif
}

void AERenderingEngine::initializeRenderSubsytem( void )
{
	m_renderer = m_root->getRenderSystemByName("OpenGL Rendering Subsystem");

	m_renderer->setConfigOption("Colour Depth",AESettingsManager::getSingleton().getSettingAsString("Graphics","Colour Depth","32"));
	m_renderer->setConfigOption("Display Frequency",AESettingsManager::getSingleton().getSettingAsString("Graphics","Display Frequency","60"));
	m_renderer->setConfigOption("FSAA",AESettingsManager::getSingleton().getSettingAsString("Graphics","FSAA","0"));
	m_renderer->setConfigOption("Full Screen",AESettingsManager::getSingleton().getSettingAsString("Graphics","Full Screen","Yes"));
	m_renderer->setConfigOption("VSync",AESettingsManager::getSingleton().getSettingAsString("Graphics","VSync","No"));
	m_renderer->setConfigOption("VSync Interval",AESettingsManager::getSingleton().getSettingAsString("Graphics","VSync Interval","1"));
	m_renderer->setConfigOption("Video Mode",AESettingsManager::getSingleton().getSettingAsString("Graphics","Video Mode","1024x768"));

	m_root->setRenderSystem(m_renderer);
}

void AERenderingEngine::initializeWindow( void )
{
	m_window = m_root->initialise(true,VER_PRODUCTNAME_STR);
	m_window->setDeactivateOnFocusChange(true);
	m_window->setAutoUpdated(true);
	m_has_focus = EWF_YES;
}

void AERenderingEngine::setIcon(void)
{
	HWND hwnd;
	m_window->getCustomAttribute("WINDOW", &hwnd);
	HINSTANCE hInst = (HINSTANCE)GetModuleHandle(NULL);
	SetClassLong (hwnd, GCL_HICON,
		(LONG)LoadIcon (hInst, MAKEINTRESOURCE (IDI_ICON1)));
}

void AERenderingEngine::windowFocusChange( Ogre::RenderWindow* _rw )
{
	if(_rw->isFullScreen())
	{
		HWND hwnd;
		m_window->getCustomAttribute("WINDOW", &hwnd);

		if(m_has_focus == EWF_YES)
		{
			ShowWindow(hwnd,SW_SHOWMINNOACTIVE);
			_rw->setActive(false);
			m_has_focus = EWF_NO;
		}
		else if(m_has_focus == EWF_NO)
		{
			_rw->setActive(true);
			m_has_focus = EWF_YES;
		}
		else
		{
			if(_rw->isActive())
				m_has_focus = EWF_YES;
			else
				m_has_focus = EWF_NO;
		}
	}
}
	}
}